/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.entity.PatMasterIndex;
import com.jims.master.modules.doctor.entity.PatVisit;
import com.jims.master.modules.doctor.service.PatMasterIndexService;
import com.jims.master.modules.med.entity.MedicalVsPatient;
import com.jims.master.modules.reg.entity.ClinicForRegist;
import com.jims.master.modules.reg.service.ClinicForRegistService;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 病人基本信息Controller
 *
 * @author 赵宁
 * @version 2017-02-28
 */
@Controller
@RequestMapping(value = "${adminPath}/oper/patMasterIndex")
public class PatMasterIndexController extends BaseController {

    @Autowired
    private PatMasterIndexService patMasterIndexService;
    @Autowired
    private ClinicForRegistService clinicForRegistService;

    @ModelAttribute
    public PatMasterIndex get(@RequestParam(required = false) String id) {
        PatMasterIndex entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = patMasterIndexService.get(id);
        }
        if (entity == null) {
            entity = new PatMasterIndex();
        }
        return entity;
    }

    /**
     * 当日挂号页面
     *
     * @return
     * @author 赵宁
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"todayRegister", ""})
    public String todayRegister(ClinicForRegist cRegist, Model model) {
        String clinicName = cRegist.getClinicLabel();
        if(StringUtils.isNotBlank(clinicName)){
            clinicName=clinicName.toUpperCase();
        }
        String deptId = cRegist.getDeptId();
        if(StringUtils.isNotBlank(deptId)){
            deptId=deptId.toUpperCase();
        }

        List<ClinicForRegist> list = clinicForRegistService.findListToday(clinicName, deptId,UserUtils.getOrgId());
        model.addAttribute("list", list);
        model.addAttribute("cRegist", cRegist);
        return "modules/reg/todayRegist";
    }

    /**
     * 保存挂号数据
     * @param clinicIds
     * @param patMasterIndex
     * @param model
     * @return  List<ClinicMaster>
     * @author 赵宁
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public List<ClinicMaster> save(String clinicIds, String prices,PatMasterIndex patMasterIndex, String payType,Model model) {
        User user = UserUtils.getUser();
        List<ClinicMaster> clinicMasters = patMasterIndexService.save(patMasterIndex, clinicIds, user,prices,payType);
        return clinicMasters;
    }
    /**
     * 门诊挂号打印
     * @param clinicMaster
     * @return StringData
     * @author 赵宁
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "printRegisit")
    @ResponseBody
    public StringData printRegisit(ClinicMaster clinicMaster){
        StringData stringData = new StringData();
        String printPath =patMasterIndexService.printRegisit(clinicMaster.getId());
        stringData.setCode("success");
        stringData.setData(printPath);
        return stringData;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(PatMasterIndex patMasterIndex, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<PatMasterIndex> page = patMasterIndexService.findPage(new Page<PatMasterIndex>(request, response), patMasterIndex);
        model.addAttribute("page", page);
        return "modules/reg/patMasterIndexIndex";
    }
    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(PatMasterIndex patMasterIndex) {
        StringData data = new StringData();
        int i = patMasterIndexService.delete(patMasterIndex);
        if (i > 0) {
            data.setCode("success");
            data.setData("删除病人基本信息成功");
        } else {
            data.setCode("error");
            data.setData("删除病人基本信息失败");
        }
        return data;
    }


    /**
     * @Author chenxy
     * @desc 更新病人信息
     * @param patMasterIndex
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findById", ""})
    public String findById(String patientId,String clinicId,String visitId,HttpServletRequest request, HttpServletResponse response, Model model) {
        PatMasterIndex patMasterIndex = this.patMasterIndexService.get(patientId);
        model.addAttribute("patMasterIndex", patMasterIndex);
        return "modules/doctor/updateOutpPatInfo";
    }
    @RequiresPermissions("user")
    @RequestMapping(value = {"prescription", ""})
    public String prescription(PatMasterIndex patMasterIndex, HttpServletRequest request, HttpServletResponse response, Model model) {

        return "modules/doctor/perscription";
    }

    /**
     * 门诊修改病人信息
     * @param patMasterIndex
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "updateInfo")
    @ResponseBody
    public StringData saveInfo(PatMasterIndex patMasterIndex, Model model) {
        StringData data = new StringData();
        int i = patMasterIndexService.updateInfo(patMasterIndex);
        if(i>0){
            data.setCode("success");
            data.setData("修改病人信息成功！");
        }else{
            data.setCode("error");
            data.setData("修改病人信息失败！");
        }
        return data;
    }

    /**
     * 当日挂号，检索病人列表
     * @param model
     * @param name
     * @param patMasterIndex
     * @param request
     * @param response
     * @return
     * @author 赵宁
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "retrievePatMaster")
    public String retrievePatMaster(Model model,String name,PatMasterIndex patMasterIndex,
                                    HttpServletRequest request,HttpServletResponse response){
        patMasterIndex.setName(name);
        Page<PatMasterIndex> page=patMasterIndexService.findPage(new Page<PatMasterIndex>(request,response),patMasterIndex);
        model.addAttribute("page",page);
        return "/modules/reg/retrievePatList";
    }

    /**
     * 患者入院登记处-取消登记功能
     * @param patMasterIndex
     * @return
     * @author CTQ
     * @date 2017-07-19 10:19:24
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "cancelRegister")
    @ResponseBody
    public StringData cancelRegister(PatMasterIndex patMasterIndex) {
        StringData data = patMasterIndexService.cancelRegister(patMasterIndex);
        return data;
    }

}